<template>
  <div class="home">
    <el-container class="containerBox">
      <!-- 头部区域 -->
      <el-header>
        <div class="title">
          <img src="../assets/heima.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <!-- 左侧区域 -->
        <el-aside :width="isCollapse?'64px':'200px'">
          <!-- 折叠按钮 -->
          <div class="toggle-button" @click="collapseFun">|||</div>
          <el-menu :default-active="activeIndex" router background-color="#333744" text-color="#fff"  unique-opened :collapse="isCollapse" :collapse-transition="false" >
          <!-- <el-menu active-text-color="#ffd04b" background-color="#333744" :collapse="isCollapse" text-color="#fff" :collapse-transition="false" unique-opened> -->
            <!-- 一级菜单 -->
            <!-- item.id转化为字符串  +'' -->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <!-- iconsObj[item.id]  得到的是对象的值 -->
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" @click="saveNavStatus(subItem.path)" :key="subItem.id">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 内容区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      activeIndex: '', // 当前激活的菜单项
      menuList: {}, // 左侧列表
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-lock_fill'
      },
      isCollapse: false
    }
  },
  name: 'Home',
  components: {},
  created () {
    this.getMenuList()
    this.activeIndex = window.sessionStorage.getItem('index')
  },
  methods: {
    // 退出登录
    logout () {
      // 清除token  跳转到登录页
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      console.log(111)
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 左侧菜单数据
      this.menuList = res.data
    },
    collapseFun () {
      this.isCollapse = !this.isCollapse
    },
    saveNavStatus (activeIndex) {
      window.sessionStorage.setItem('index', activeIndex)
    }
  }
}
</script>
<style lang="less" scoped>
.containerBox {
  width: 100%;
  height: 100%;
}
.home {
  width: 100%;
  height: 100%;
  // 头部
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .title {
      color: #ffff;
      font-size: 30px;
      display: flex;
      align-items: center;
      span {
        margin-left: 30px;
      }
    }
  }
  .el-aside {
    width: 100%;
    height: 100%;
    background-color: #333744;
  }
  .el_main {
    width: 100%;
    height: 100%;
    background-color: #eaedf1;
  }
  .iconfont {
    margin-right: 10px;
  }
  // 折叠按钮
  .toggle-button {
    width: 100%;
    padding: 6px 0;
    background-color: #4A5064;
    font-size: 14px;
    text-align: center;
    letter-spacing: 4px;
    color: #fff;
  }
}
</style>
